package com.fruit.interceptor;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fruit.constants.RedisConstant;
import com.fruit.service.redis.UserRedisUtil;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

/**
 * app登录Interceptor
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
public class AppLoginSecurityInterceptor extends HandlerInterceptorAdapter {

	@Value("${app.key}")
	private String key;

	@Autowired
	private UserRedisUtil userRedisService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		response.setContentType("text/html;charset=UTF-8");
		Map<String, Object> resMap = new HashMap<String, Object>();
		ObjectMapper om = new ObjectMapper();
		try {
			int loginId = Integer.parseInt(request.getParameter("loginId"));
			String token = request.getParameter("token");
			if (loginId <= 0 || StringUtils.isEmpty(token)) {
				ResultInfo ri = ResultUtil.initResult(ResultInfo.ERROR, "params_missing", "参数丢失");
				ri.setIsAgainLogin("01");
				resMap.put("result", ri);
				response.getWriter().print(om.writeValueAsString(resMap));
				return false;
			}

			if (!userRedisService.isExists(RedisConstant.APP_USER_TOOKEN + loginId)) {
				ResultInfo ri = ResultUtil.initResult(ResultInfo.ERROR, "token_invalid", "登录失效");
				ri.setIsAgainLogin("02");
				resMap.put("result", ri);
				response.getWriter().print(om.writeValueAsString(resMap));
				return false;
			}
			String myToken = userRedisService.getUserToken(RedisConstant.APP_USER_TOOKEN + loginId);
			if (!StringUtils.equals(token, myToken)) {
				ResultInfo ri = ResultUtil.initResult(ResultInfo.ERROR, "token_invalid", "异地登录");
				ri.setIsAgainLogin("01");
				resMap.put("result", ri);
				response.getWriter().print(om.writeValueAsString(resMap));
				return false;
			}

			/*
			 * Map<String, Object> params = new HashMap<String, Object>();
			 * params.put("loginId", loginId); params.put("token", token);
			 * UserLogin ul = userService.getUserLoginByToken(params); if(ul ==
			 * null){ ResultInfo ri = ResultUtil.initResult(ResultInfo.ERROR,
			 * "token_invalid", "登录失效"); ri.setIsAgainLogin("01");
			 * resMap.put("result", ri);
			 * response.getWriter().print(om.writeValueAsString(resMap)); return
			 * false; }
			 * 
			 * long curTime = System.currentTimeMillis(); if(curTime >
			 * ul.getTokenLoseTime()){ ResultInfo ri =
			 * ResultUtil.initResult(ResultInfo.ERROR, "token_lose", "登录失效");
			 * ri.setIsAgainLogin("01"); resMap.put("result", ri);
			 * response.getWriter().print(om.writeValueAsString(resMap)); return
			 * false; }
			 */

		} catch (Exception e) {
			e.printStackTrace();
			ResultInfo ri = ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常");
			ri.setIsAgainLogin("02");
			resMap.put("result", ri);
			response.getWriter().print(om.writeValueAsString(resMap));
			return false;
		}

		return true;

	}

}
